set(CMAKE_VERBOSE_MAKEFILE yes)
add_subdirectory(../../src src)

find_package(PkgConfig REQUIRED)
pkg_check_modules(AVCODEC REQUIRED libavcodec)
pkg_check_modules(AVFORMAT REQUIRED libavformat)
pkg_check_modules(AVUTIL REQUIRED libavutil)
pkg_check_modules(SWR REQUIRED libswresample)
pkg_check_modules(GIO_UNIX REQUIRED gio-unix-2.0)

add_definitions(-Werror=implicit-function-declaration)

message("AVCODEC_LDFLAGS=${AVCODEC_LDFLAGS}")
message("AVCODEC_STATIC_LIBRARIES=${AVCODEC_STATIC_LIBRARIES}")
message("AVCODEC_STATIC_OTHER_LDFLAGS=${AVCODEC_STATIC_OTHER_LDFLAGS}")

set(BENCH_SOURCES
        ${CMAKE_CURRENT_SOURCE_DIR}/Audio.hpp
        ${CMAKE_CURRENT_SOURCE_DIR}/subprocess.cpp
        ${CMAKE_CURRENT_SOURCE_DIR}/subprocess.hpp
        ${CMAKE_CURRENT_SOURCE_DIR}/child.cpp
        ${CMAKE_CURRENT_SOURCE_DIR}/child.hpp
        ${CMAKE_CURRENT_SOURCE_DIR}/clock.cpp
        ${CMAKE_CURRENT_SOURCE_DIR}/clock.hpp
        ${CMAKE_CURRENT_SOURCE_DIR}/conn.cpp
        ${CMAKE_CURRENT_SOURCE_DIR}/conn.hpp
        ${CMAKE_CURRENT_SOURCE_DIR}/config.cpp
        ${CMAKE_CURRENT_SOURCE_DIR}/config.hpp
        ${CMAKE_CURRENT_SOURCE_DIR}/RtpHeader.hpp
        ${CMAKE_CURRENT_SOURCE_DIR}/http.cpp
        ${CMAKE_CURRENT_SOURCE_DIR}/Http.hpp
        ${CMAKE_CURRENT_SOURCE_DIR}/log.hpp
        ${CMAKE_CURRENT_SOURCE_DIR}/master.cpp
        ${CMAKE_CURRENT_SOURCE_DIR}/master.hpp
        ${CMAKE_CURRENT_SOURCE_DIR}/metric.hpp
        ${CMAKE_CURRENT_SOURCE_DIR}/metric_client.cpp
        ${CMAKE_CURRENT_SOURCE_DIR}/metric_client.hpp
        ${CMAKE_CURRENT_SOURCE_DIR}/metric_server.cpp
        ${CMAKE_CURRENT_SOURCE_DIR}/metric_server.hpp
        ${CMAKE_CURRENT_SOURCE_DIR}/ping.cpp
        ${CMAKE_CURRENT_SOURCE_DIR}/ping.hpp
        ${CMAKE_CURRENT_SOURCE_DIR}/Timeline.hpp
        ${CMAKE_CURRENT_SOURCE_DIR}/stat.hpp
        ${CMAKE_CURRENT_SOURCE_DIR}/report.cpp
        ${CMAKE_CURRENT_SOURCE_DIR}/report.hpp
        )

add_executable(bench
        ${RTC_XSWITCH_SOURCES}
        ${RTP_SOURCES}
        ${BENCH_SOURCES}
        main_benchmark.cpp)

# PRIVATE的意思是这个目录是非传递的
target_include_directories(bench PRIVATE
        ${GIO_UNIX_INCLUDE_DIRS})

#add_executable(html
#        main_html.cpp
#        report.hpp
#        )
link_directories(
        ${AVCODEC_LIBRARY_DIRS}
        ${AVFORMAT_LIBRARY_DIRS}
        ${AVUTIL_LIBRARY_DIRS}
        ${SWR_LIBRARY_DIRS}
        ${CMAKE_SOURCE_DIR}/stage/lib/
)

# todo: it is wrong to use ldflags here,
# when build on mac, ldflags could contains -framework -xxx
# that link to -lframe -lxxx
target_link_libraries(bench
        ${LOG4CPLUS_LDFLAGS}
	# libglib-2.0.a
	# ${GLIB_STATIC_LIBRARIES}
        ${GLIB_LDFLAGS}
	# libgobject-2.0.a
	# ${GOBJECT_STATIC_LIBRARIES}
        ${GOBJECT_LDFLAGS}
        ${GIO_LDFLAGS}
	# ${GIO_STATIC_LIBRARIES}
	# libgio-2.0.a
	# libffi.so
	# libgmodule-2.0.a
	# -ldl
	# -lffi
	# -lselinux
	# -lresolv
	# -lmount
        ${OPENSSL_LDFLAGS}
        ${OPUS_LDFLAGS}
        ${LIBSOUP_LDFLAGS}
        ${SRTP_LDFLAGS}
        ${NICE_LDFLAGS}
        ${AVCODEC_STATIC_LIBRARIES}
        ${AVFORMAT_STATIC_LIBRARIES}
        ${AVUTIL_STATIC_LIBRARIES}
        ${SWR_STATIC_LIBRARIES}
        "-L${CMAKE_SOURCE_DIR}/third/webrtc/lib/ -laudio_recveq -pthread"
        -lmp3lame
	-ldl
        )

#add_executable(timer main_timer.cpp)
#
#target_link_libraries(timer ${GLIB_LDFLAGS}
#        ${GOBJECT_LDFLAGS}
#        ${GIO_LDFLAGS})


if (APPLE)
    set_target_properties(bench PROPERTIES LINK_FLAGS "-framework CoreFoundation -framework CoreServices -framework QuartzCore -framework VideoDecodeAcceleration")
endif (APPLE)

if (LIBEXECINFO_STANDALONE)
    target_link_libraries(bench -lexecinfo)
endif(LIBEXECINFO_STANDALONE)
include(GetPrerequisites)

install(TARGETS bench DESTINATION bin)
set(BINARY_LOCATION 
        "${CMAKE_INSTALL_PREFIX}/bin/bench")

get_prerequisites(${BINARY_LOCATION} DEPENDENCIES 0 0 "" "")
foreach(DEPENDENCY_FILE ${DEPENDENCIES})
  get_filename_component(DEPENDENCY_NAME "${DEPENDENCY_FILE}" NAME)
  get_filename_component(DEPENDENCY "${DEPENDENCY_FILE}" REALPATH)
  get_filename_component(DEPENDENCY_PATH "${DEPENDENCY}" DIRECTORY)
  install(FILES ${DEPENDENCY} DESTINATION "${DEPENDENCY_PATH}" )
endforeach()
